﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_bkgl_stuimg : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    public string wbh;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            wbh = Request.QueryString["wbh"].ToString();
            var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                          select p).ToList().First();
            Image2.ImageUrl = "~/Student/stuimage/" + result.sidfy + ".jpg";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        wbh = Request.QueryString["wbh"].ToString();
        var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                      select p).ToList().First();
        if (FileUpload1.HasFile)
        {
            try
            {
                string delFile = Server.MapPath("~/Student/stuimage/" + result.sidfy + ".jpg");
                //删除图片 
                //File.Delete(delFile);
                string filename = Path.GetFileName(FileUpload1.FileName);
                string strEName = filename.Substring(filename.LastIndexOf(".") + 1, (filename.Length - filename.LastIndexOf(".") - 1));
                if (strEName != "jpg" && strEName != "JPG")
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('请上传jpg格式的图片！')</script>");
                    return;
                }
                FileUpload1.SaveAs(Server.MapPath("~/Student/stuimage/") + result.sidfy + ".jpg");
                Image2.ImageUrl = "~/Student/stuimage/" + result.sidfy + ".jpg";
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('上传失败，请重新上传！')</script>");
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        wbh = Request.QueryString["wbh"].ToString();
        var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                      select p).ToList().First();
        if (FileUpload1.HasFile)
        {
            try
            {
                string delFile = Server.MapPath("~/Student/stuimage/" + result.sidfy + ".jpg");
                //删除图片 
                File.Delete(delFile);
                string filename = Path.GetFileName(FileUpload1.FileName);
                string strEName = filename.Substring(filename.LastIndexOf(".") + 1, (filename.Length - filename.LastIndexOf(".") - 1));
                if (strEName != "jpg")
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('请上传jpg格式的图片！')</script>");
                    return;
                }
                FileUpload1.SaveAs(Server.MapPath("~Student/stuimage/") + result.sidfy + ".jpg");
                Image2.ImageUrl = "~/Student/stuimage/" + result.sidfy + ".jpg";
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('上传失败，请重新上传！')</script>");
            }
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.location='tj.aspx?wbh=" + wbh + "'</script>");
    }
}